//
//  AIGirlNavViewController.swift
//  AIGirl
//
//  Created by Rae on 2023/12/22.
//

import UIKit
import EachNavigationBar

class AIGirlNavViewController: UINavigationController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigation.configuration.isEnabled = true
        // 移除毛玻璃效果
        navigation.configuration.isTranslucent = false
        // 隐藏底部阴影
        navigation.configuration.isShadowHidden = true
        
        navigation.configuration.titleTextAttributes = [NSAttributedString.Key.font: 20.font_bold ?? UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.black]
        
        navigation.configuration.backItem = UINavigationController.Configuration.BackItem(style: .image("FJ_Common_back".toImage?.withRenderingMode(.alwaysOriginal)))
        
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        var controllerIDs = [String]()
        viewControllers.forEach { item in
            if let controller = item as? AIGirlBaseViewController {
                controllerIDs.append(controller.controller_identification())
            }
        }
        let currentController = viewController as? AIGirlBaseViewController
        if let controllerID = controllerIDs.filter({ $0 == currentController?.controller_identification() }).first {
            for item in viewControllers {
                if let controller = item as? AIGirlBaseViewController, controller.controller_identification() == controllerID {
                    popToViewController(item, animated: true)
                }
            }
            return
        }
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    override var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        super.popViewController(animated: true)
        return self.topViewController
    }
    
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        if animated {
            let popController = self.viewControllers.last
            popController?.hidesBottomBarWhenPushed = false
        }
        return super.popToRootViewController(animated: animated)
    }

}

extension UIViewController {
    
    @discardableResult
    func title(_ title: String?) -> Self {
        navigation.item.title = title
        return self
    }
    
    @discardableResult
    func titleView(_ view: UIView?) -> Self {
        navigation.item.titleView = view
        return self
    }
    
//    @discardableResult
//    func leftBarButton(_ title: String?, color: String, selector: Selector?) -> Self {
//        let barButton = s_make(UIButton.self).alphaGo.color(color)
//            .text(title).font(22.font_heavy).end()
//        if let selector {
//            barButton.alphaGo.addTarget(self, selector: selector)
//        }
//        navigation.item.leftBarButtonItem = UIBarButtonItem(customView: barButton)
//        return self
//    }
    
    @discardableResult
    func leftBarButton(_ imageName: String?, selector: Selector?) -> Self {
        let button = UIBarButtonItem(image: imageName?.toImage?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: selector)
        navigation.item.leftBarButtonItem = button
        return self
    }
    
    @discardableResult
    func leftBarButton(_ barButton: UIBarButtonItem) -> Self {
        navigation.item.leftBarButtonItem = barButton
        return self
    }
    
    @discardableResult
    func leftBarButtons(_ barButtons: [UIBarButtonItem]) -> Self {
        navigation.item.leftBarButtonItems = barButtons
        return self
    }
    
    @discardableResult
    func rightBarButton(_ title: String?, color: String, selector: Selector?) -> Self {
        let button = UIBarButtonItem(title: title, style: .plain, target: self, action: selector)
        button.tintColor = color.toColor
        navigation.item.rightBarButtonItem = button
        return self
    }
    
    @discardableResult
    func rightBarButton(_ imageName: String?, selector: Selector?) -> Self {
        let button = UIBarButtonItem(image: imageName?.toImage?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: selector)
        navigation.item.rightBarButtonItem = button
        return self
    }
    
    @discardableResult
    func rightBarButton(_ barButton: UIBarButtonItem) -> Self {
        navigation.item.rightBarButtonItem = barButton
        return self
    }
    
    @discardableResult
    func rightBarButtons(_ barButtons: [UIBarButtonItem]) -> Self {
        navigation.item.rightBarButtonItems = barButtons
        return self
    }
    
    
}
